﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EITSurferInfinityLib
{
    public static class Vector2BinFile
    {
        public static void Save(string name, short [] v1, short [] v2)
        {
            FileStream stream = new FileStream(name+".vec", FileMode.Create);
            BinaryWriter bi = new BinaryWriter(stream);

            int code=1;
            int NumberOfVector=2;
            int LengthV1=v1.Length;
            int LengthV2=v2.Length;

            bi.Write(code);
            bi.Write(NumberOfVector);
            bi.Write(LengthV1);
            bi.Write(LengthV2);

            for (int i = 0; i < LengthV1; i++)
            {
                bi.Write(v1[i]);
            }
            for (int i = 0; i < LengthV2; i++)
            {
                bi.Write(v2[i]);
            }  
        }

        public static void Save(string name, Int32[] v1, Int32[] v2)
        {
            FileStream stream = new FileStream(name + ".vec", FileMode.Create);
            BinaryWriter bi = new BinaryWriter(stream);

            int code = 2;
            int NumberOfVector = 2;
            int LengthV1 = v1.Length;
            int LengthV2 = v2.Length;

            bi.Write(code);
            bi.Write(NumberOfVector);
            bi.Write(LengthV1);
            bi.Write(LengthV2);

            for (int i = 0; i < LengthV1; i++)
            {
                bi.Write(v1[i]);
            }
            for (int i = 0; i < LengthV2; i++)
            {
                bi.Write(v2[i]);
            }
            stream.Close();
        }

        public static void Read(string name, out Int32[] v1, out Int32[] v2)
        {
            try
            {
                FileStream stream = new FileStream(name, FileMode.Open);
                BinaryReader bi = new BinaryReader(stream);
                int code = bi.ReadInt32();
                int NumberOfVector = bi.ReadInt32();
                List<int> LengthVectors = new List<int>();
                for (int i = 0; i < NumberOfVector; i++)
                {
                    LengthVectors.Add(bi.ReadInt32());
                }

                v1 = new Int32[LengthVectors[0]];
                for (int i = 0; i < LengthVectors[0]; i++)
                {
                    v1[i] = bi.ReadInt32();
                }
                v2 = new Int32[LengthVectors[1]];
                for (int i = 0; i < LengthVectors[1]; i++)
                {
                    v2[i] = bi.ReadInt32();
                }
                bi.Close();
                stream.Close();
            }
            catch (Exception e)
            {
                v1 = new Int32[0];
                v2 = new Int32[0];
            }
        }

        public static string GetIncrementalName(string dir, string name)
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            if(di.Exists==false)
            {
                System.IO.Directory.CreateDirectory(dir);
            }
                
            FileInfo[] fi = di.GetFiles();

            int maxFileIndex = -1;

            for (int i = 0; i < fi.Length; i++)
            {
                string s = fi[i].Name;
                if (s.Contains("_"))
                {
                    string[] split = s.Split('_');
                    split = split[1].Split('.');
                    int FileIndex = Convert.ToInt32(split[0]);
                    if (maxFileIndex < FileIndex)
                    {
                        maxFileIndex = FileIndex;
                    }
                }
                
            }
            maxFileIndex++;
            return name + "_" + maxFileIndex.ToString();
        }
    }
}
